package com.thoughtworks.android.androsphere.service.c2dm;

import org.apache.log4j.Logger;

public enum C2DMErrorResponseEnum {
    QUOTA_EXCEEDED("QuotaExceeded") {
        @Override
        public C2DMResponse process(C2DMProcessor c2DMProcessor) {
            logger.debug("For C2DM Response 200 - QuotaExceeded");
            c2DMProcessor.waitAndRetry();
            return C2DMResponse.PROCEED;
        }},
    NOT_REGISTERED("NotRegistered") {
        @Override
        public C2DMResponse process(C2DMProcessor c2DMProcessor) {
            logger.debug("For C2DM Response 200 - NotRegistered");
            c2DMProcessor.removeRegistrationId();
            return C2DMResponse.PROCEED;
        }},
    DEVICE_QUOTA_EXCEEDED("DeviceQuotaExceeded") {
        @Override
        public C2DMResponse process(C2DMProcessor c2DMProcessor) {
            logger.debug("For C2DM Response 200 - DeviceQuotaExceeded");
            logger.debug("Implementing Backoff - DEFER");
            return C2DMResponse.DEFER;
        }},
    MESSAGE_TOO_BIG("MessageTooBig") {
        @Override
        public C2DMResponse process(C2DMProcessor c2DMProcessor) {
            logger.debug("For C2DM Response 200 - MessageTooBig");
            return C2DMResponse.PROCEED;
        }},
    MISSING_COLLAPSE_KEY("MissingCollapseKey") {
        @Override
        public C2DMResponse process(C2DMProcessor c2DMProcessor) {
            logger.debug("For C2DM Response 200 - MissingCollapseKey");
            return C2DMResponse.PROCEED;
        }},
    INVALID_REGISTRATION("InvalidRegistration") {
        @Override
        public C2DMResponse process(C2DMProcessor c2DMProcessor) {
            logger.debug("For C2DM Response 200 - InvalidRegistration");
            c2DMProcessor.removeRegistrationId();
            return C2DMResponse.PROCEED;
        }};

    private static final Logger logger = Logger.getLogger(C2DMErrorResponseEnum.class);
    private String description;

    C2DMErrorResponseEnum(String description) {
        this.description = description;
    }

    public abstract C2DMResponse process(C2DMProcessor c2DMProcessor);

    public static C2DMErrorResponseEnum getType(String errorCode) {
        for (C2DMErrorResponseEnum errorCodeEnum : C2DMErrorResponseEnum.values()) {
            if (errorCodeEnum.description.equalsIgnoreCase(errorCode)) {
                return errorCodeEnum;
            }
        }
        return null;
    }
}
